package org.tsp.android.GUI_Menus;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Currency;

import org.tsp.android.GUI_Menus.R;
import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class GUI_MenusActivity extends Activity {
    /** Called when the activity is first created. */
	
	private Boolean text_bold = false;
	private Boolean text_italic = false;
	private int image_index = 0;
	
	private Handler mHandler = new Handler();
	
	private Boolean slideshow_started = false;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
    }
	
	@Override
	public void onResume()
	{
		super.onResume();

		if ( slideshow_started )
			mHandler.postDelayed(mUpdateTimeTask, 3000);
	}
	
	@Override 
	public void onPause()
	{
		super.onPause();
		
		mHandler.removeCallbacks(mUpdateTimeTask);
	}
	
	@Override
	public void onDestroy()
	{
		super.onDestroy();
		
	}
    
    public void loadImageClick(View v)
    {
    	loadImage(R.drawable.ic_launcher);
    }   
    
    private void loadImage()
    {
    	loadImage(R.drawable.ic_launcher);
    }
    private void loadImage(int idImage)
    {
    	Bitmap aBitmap = BitmapFactory.decodeResource(getResources(),idImage);
    	loadBmp(aBitmap);
    }
    private void loadBmp(Bitmap aBitmap){
    	ImageView aImageView = (ImageView) findViewById(R.id.img_view);
    	aImageView.setImageBitmap(aBitmap);    	
    }
    
    
    public void startSlidesowClick(View v)
    {    
        startSlidesow();        
    }

	private void startSlidesow()
    {
    	slideshow_started = true;
    	mHandler.removeCallbacks(mUpdateTimeTask);
        mHandler.postDelayed(mUpdateTimeTask, 3000);    
    }

    private Runnable mUpdateTimeTask = new Runnable() {
 	   public void run() {
 		   String[] files;
 		   mHandler.postDelayed(mUpdateTimeTask, 3000);
 		   try {
 			AssetManager assetManager = getAssets();
 			files = assetManager.list("Images");
 			if ( files.length == 0 )
 				return;
 			loadImageAssets("Images"+ File.separator + files[image_index++]);
 			if ( image_index == files.length )
 				image_index = 0;
 		   } catch (IOException e) {
 			   e.printStackTrace();
 		   }	
 	   }
 	};
    
    public void btn_next_Activity_Click(View v){
    	btn_next_Activity();
    }
    private void btn_next_Activity(){
    	startActivity(new Intent(GUI_MenusActivity.this , Activity_2.class));
    }
    
    private void loadImageAssets(String image)
    {
    	
		try {
			InputStream is = getResources().getAssets().open(image);		
			Bitmap aBitmap = BitmapFactory.decodeStream(is);
			loadBmp(aBitmap);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }    

       
    @Override 
    public boolean onCreateOptionsMenu(Menu menu)
    {
    	MenuInflater inf = getMenuInflater();
    	inf.inflate(R.menu.main_menu, menu);
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
    	switch (item.getItemId())
    	{
    		case R.id.menu_exit:
    			this.finish();
    			return true;

    			// To Complete
    			// Change the checked state of the menu item
    			// and update the text style
    		case R.id.menu_Load_image:
    			loadImage();
    			return true;
    		case R.id.menu_ShowSlide:
    			startSlidesow();
    			return true;
    		case R.id.menu_Next_activity:
    			btn_next_Activity();
    			return true;
    		case R.id.menu_bold:
    			text_bold = !item.isChecked();
    			changeStyleButton();
    			item.setChecked(!item.isChecked());	
    			return true;
    		case R.id.menu_italic:
    			text_italic = !item.isChecked();
    			changeStyleButton();
    			item.setChecked(!item.isChecked());	
    			return true;
    		default: 
    			return super.onOptionsItemSelected(item);
    	}
    }
    private void changeStyleButton()
    {
		Button aButton1 = (Button) findViewById(R.id.btn_load_image);
		Button aButton2 = (Button) findViewById(R.id.btn_start_slideshow);
		Button aButton3 = (Button) findViewById(R.id.btn_next_Activity);
		int style = 0 ;
		if(text_bold && text_italic)
		{			
			style = R.style.BoldItalicButtonFont;	
		}else if(text_bold && !text_italic ){
			style = R.style.BoldButtonFont;
		}else if(!text_bold && text_italic ){
			style = R.style.ItalicButtonFont;
		}else if(!text_bold && !text_italic ){
			style = R.style.ButtonFont;
		}
		aButton1.setTextAppearance(this, style) ;
		aButton2.setTextAppearance(this, style);
		aButton3.setTextAppearance(this, style);
    }

}